<?php

namespace App\Http\Middleware;

use App\MidAutumnLog;
use App\User;
use App\Wechat;
use Closure;

/**
 * Class HasTokenMiddleware
 * @package App\Http\Middleware
 */
class HasTokenMiddleware
{

    /**
     * @param $request
     * @param Closure $next
     * @return \Illuminate\Http\JsonResponse|mixed
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     */
    public function handle($request, Closure $next)
    {
        if (!$request->has('token')) {
            return response()->json(error('没有token'));
        }
        //校验id存在不存在
        $user_id = hashid_decode($request->token);
        if (!$open_id = User::where('c_id', $user_id)->value('wx_open_id')) {
            return response()->json(error('你是谁哦？'));
        }
        return $next($request);//Wechat::IsFollow($open_id) ? $next($request) : response()->json(error('请关注公众号', 404));
    }
}
